package base.thread.create;

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @Author: my_code
 * @Email: my_code@foxmail.com
 * @Create: 2020-12-27 15:02
 */
public class ThreadTest3 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main---start----");
        FutureTask<Date> futureTask = new FutureTask<>(new Callable01());
        new Thread(futureTask).start();
        Date date = futureTask.get();//阻塞获取异步执行的线程
        System.out.println(date);
        System.out.println("main---end----");
    }

    public static class Callable01 implements Callable<Date>{

        @Override
        public Date call(){
            System.out.println("进入callable");
            Date date = new Date();
            System.out.println("业务处理");
            //延时几秒
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("退出callable");
            return date;
        }
    }
}
